using System;
using BruteForce.Characters;
using BruteForce.GameObjects;
using BruteForce.NewManagers;
using BruteForce.UserInterface.Abstracts;
using BruteForce.UserInterface.UIControls;
using BruteForce.Utilities;
using Bruteforce.UserInterface.UIControls;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

namespace BruteForce.Screens.PlayingScreen.Panels {
    public class CharacterStatusPanel : AbstractPanel {
        public CharacterStatusPanel()
            : base("CharacterStatusPanel") {
        }

        public override void Update(GameTime gameTime) {
            foreach (AbstractUIControl uiControl in Children) {
                uiControl.Update();
            }
        }

        public override void Draw(GameTime gameTime, SpriteBatch spriteBatch) {
            spriteBatch.End();
            spriteBatch.Begin(SpriteSortMode.FrontToBack, BlendState.Opaque, SamplerState.LinearWrap,
                              DepthStencilState.Default, RasterizerState.CullNone);
            spriteBatch.Draw(UIControlManager.Instance.GreyStoneTexture, new Vector2(Bounds.X, Bounds.Y), Bounds,
                             Color.White, 0, Vector2.Zero, 1, SpriteEffects.None, 0);
            spriteBatch.End();
            spriteBatch.Begin();
            Draws.DrawBorder(Bounds, new Color(91, 91, 91), 2);

            foreach (AbstractUIControl uiControl in Children) {
                uiControl.Draw(gameTime);
            }
        }

        //public override void Draw(GameTime gameTime) {

        //    Draws.DrawBorder(Bounds, Color.Yellow, 2);

        //    foreach (AbstractUIControl uiControl in Children) {
        //        uiControl.Draw(gameTime);
        //    }


        //}


        public override void AfterLayout() {
            const int diffBetweenUIcontents = 10;
            var img = GetCharacterImg();
            var imageContent = new DynamicImage(img, "Character Image",
                                         new Rectangle(
                                             (Bounds.Width - img.Width) / 2,
                                             diffBetweenUIcontents, img.Width,
                                             img.Height), GetCharacterImg);
            imageContent.SetOffset(new Vector2(Bounds.X, Bounds.Y));
            AddUIControl(imageContent);
            int diffY = img.Height + diffBetweenUIcontents;

            int textLabelHight;

            var diffBetweenLines = CreaterCharacterRecourcesStatusLabels(diffY, out textLabelHight);

            //diffY = CreaterRecourcesLeftForTurnLabels(textLabelHight, diffY, ref diffBetweenLines);


            CreateButtons();

            CreateMouseOverLabels(textLabelHight, diffBetweenLines, diffY);
        }

        private void CreateButtons() {
            Texture2D image = UIControlManager.Instance.WalkButtonTexture;
            Texture2D image2 = UIControlManager.Instance.WalkPressedButtonTexture;
            var dynamicButton = new DynamicButton(image, image2, "walk Button",
                                                  new Rectangle((Bounds.Width - image.Width) / 2,
                                                                (Bounds.Height / 2) - 30, image.Width,
                                                                image.Height), CanWalk);
            dynamicButton.SetOffset(new Vector2(Bounds.X, Bounds.Y));
            dynamicButton.OnClick += (() => GamePanel.Current.HandleWalkingPressedButton());
            AddUIControl(dynamicButton);



            image = UIControlManager.Instance.ShootButtonTexture;
            image2 = UIControlManager.Instance.ShootPressedButtonTexture;
            dynamicButton = new DynamicButton(image, image2, "shoot Button",
                                                  new Rectangle((Bounds.Width - image.Width) / 2,
                                                                (Bounds.Height / 2 + image.Height) - 30, image.Width,
                                                                image.Height), CanShoot);
            dynamicButton.SetOffset(new Vector2(Bounds.X, Bounds.Y));
            dynamicButton.OnClick += (() => GamePanel.Current.HandleShootingPressedButton());
            AddUIControl(dynamicButton);



            image = UIControlManager.Instance.DismissButtonTexture;
            image2 = UIControlManager.Instance.DismissPressedButton;
            dynamicButton = new DynamicButton(image, image2, "Dismiss Button",
                                                  new Rectangle((Bounds.Width - image.Width) / 2,
                                                                (Bounds.Height / 2 + 2 * image.Height) - 30, image.Width,
                                                                image.Height), canDismiss);
            dynamicButton.SetOffset(new Vector2(Bounds.X, Bounds.Y));
            dynamicButton.OnClick += (() => GamePanel.Current.HandleDismissPressedButton());
            AddUIControl(dynamicButton);
        }

        private bool canDismiss() {
            return (!GamePanel.Current.gameOver && PlayerManager.Instance.CurrentPlayer.Property.CurrentCharacter != null &&
                    PlayerManager.Instance.CurrentPlayer.Property.CurrentCharacter != NullCharacter.Instance);
        }

        private bool CanShoot() {
            return !GamePanel.Current.gameOver && PlayerManager.Instance.CurrentPlayer.Property.CurrentCharacter.CurrentResources.Power > 0;
        }

        private bool CanWalk() {
            return !GamePanel.Current.gameOver && PlayerManager.Instance.CurrentPlayer.Property.CurrentCharacter.CurrentResources.Speed > 0;
        }


        private Vector2 CreaterCharacterRecourcesStatusLabels(int diffY, out int textLabelHight) {
            //********************************** "Resources Status of the character"  ******************************************//

            //create "Unit Status :" label
            string labelText = "Unit Status";
            Vector2 diffBetweenLines = Draws.CalculateWordSizeNormalFont(labelText);
            int midPanelFotText = (int)(Bounds.Width - diffBetweenLines.X) / 2;
            textLabelHight = (int)diffBetweenLines.Y;
            var Statuslabel = new Label("Unit Status label", labelText,
                                        new Rectangle(midPanelFotText, diffY, (int)diffBetweenLines.X,
                                                      textLabelHight));
            Statuslabel.SetOffset(new Vector2(Bounds.X, Bounds.Y));
            AddUIControl(Statuslabel);


            //create "      Life :" label
            labelText = "Life : ";
            diffBetweenLines = Draws.CalculateWordSizeNormalFont(labelText);
            midPanelFotText = (int)(Bounds.Width - diffBetweenLines.X) / 2;

            var label = new InteractiveLabel("Life label", labelText,
                                             new Rectangle(midPanelFotText, diffY + 2 * textLabelHight, (int)diffBetweenLines.X,
                                                           textLabelHight), GetPlayerCharacterLife);
            label.SetOffset(new Vector2(Bounds.X, Bounds.Y));
            label.SetLabelTextColor(Color.Black);
            AddUIControl(label);

            //create "      Light :" label
            labelText = "Light: ";
            diffBetweenLines = Draws.CalculateWordSizeNormalFont(labelText);
            midPanelFotText = (int)(Bounds.Width - diffBetweenLines.X) / 2;


            label = new InteractiveLabel("Light label", labelText,
                                         new Rectangle(midPanelFotText, diffY + 3 * textLabelHight, (int)diffBetweenLines.X,
                                                       textLabelHight), GetPlayerCharacterLight);
            label.SetOffset(new Vector2(Bounds.X, Bounds.Y));
            label.SetLabelTextColor(Color.Black);
            AddUIControl(label);

            //create "      Speed :" label
            labelText = "Speed: ";
            diffBetweenLines = Draws.CalculateWordSizeNormalFont(labelText);
            midPanelFotText = (int)(Bounds.Width - diffBetweenLines.X) / 2;


            label = new InteractiveLabel("Speed label", labelText,
                                         new Rectangle(midPanelFotText, diffY + 4 * textLabelHight, (int)diffBetweenLines.X,
                                                       textLabelHight), GetPlayerCharacterSpeed);
            label.SetOffset(new Vector2(Bounds.X, Bounds.Y));
            label.SetLabelTextColor(Color.Black);
            AddUIControl(label);

            //create "      Power :" label
            labelText = "Power: ";
            diffBetweenLines = Draws.CalculateWordSizeNormalFont(labelText);
            midPanelFotText = (int)(Bounds.Width - diffBetweenLines.X) / 2;


            label = new InteractiveLabel("Power label", labelText,
                                         new Rectangle(midPanelFotText, diffY + 5 * textLabelHight, (int)diffBetweenLines.X,
                                                       textLabelHight), GetPlayerCharacterPower);
            label.SetOffset(new Vector2(Bounds.X, Bounds.Y));
            label.SetLabelTextColor(Color.Black);
            AddUIControl(label);
            return diffBetweenLines;
        }

        private int CreaterRecourcesLeftForTurnLabels(int textLabelHight, int diffY, ref Vector2 diffBetweenLines) {
            Label label;
            string labelText;
            int midPanelFotText;
            //******************************************* "Resources left for Turn" column **********************************//
            diffY += (int)diffBetweenLines.Y;

            //create "Human Status :" label
            labelText = "Per Turn \nResources";
            diffBetweenLines = Draws.CalculateWordSizeNormalFont(labelText);
            midPanelFotText = (int)(Bounds.Width - diffBetweenLines.X) / 2;

            label = new Label("Resources left for Turn Status label", labelText,
                              new Rectangle(midPanelFotText, diffY + 6 * textLabelHight, (int)diffBetweenLines.X,
                                            textLabelHight));
            label.SetOffset(new Vector2(Bounds.X, Bounds.Y));
            AddUIControl(label);

            diffY += (int)diffBetweenLines.Y / 2;


            //            //create "      Life :" label
            //            labelText = "Life : ";
            //            diffBetweenLines = Draws.CalculateWordSizeNormalFont(labelText);
            //            midPanelFotText = (int)(Bounds.Width - diffBetweenLines.X) / 2;
            //
            //            InteractiveLabel interactiveLabel = new InteractiveLabel("Life label", labelText,
            //                                                                     new Rectangle(midPanelFotText, diffY + 7 * textLabelHight,
            //                                                                                   (int)diffBetweenLines.X,
            //                                                                                   textLabelHight), GetPlayerCharacterTurnLife);
            //            interactiveLabel.SetOffset(new Vector2(Bounds.X, Bounds.Y));
            //            interactiveLabel.SetLabelTextColor(Color.Black);
            //            AddUIControl(interactiveLabel);

            //create "      Light :" label
            labelText = "Light: ";
            diffBetweenLines = Draws.CalculateWordSizeNormalFont(labelText);
            midPanelFotText = (int)(Bounds.Width - diffBetweenLines.X) / 2;


            var interactiveLabel = new InteractiveLabel("Light interactiveLabel", labelText,
                                                    new Rectangle(midPanelFotText, diffY + 8 * textLabelHight,
                                                                  (int)diffBetweenLines.X,
                                                                  textLabelHight), GetPlayerCharacterTurnLight);
            interactiveLabel.SetOffset(new Vector2(Bounds.X, Bounds.Y));
            interactiveLabel.SetLabelTextColor(Color.Black);
            AddUIControl(interactiveLabel);

            //create "      Speed :" interactiveLabel
            labelText = "Speed: ";
            diffBetweenLines = Draws.CalculateWordSizeNormalFont(labelText);
            midPanelFotText = (int)(Bounds.Width - diffBetweenLines.X) / 2;


            interactiveLabel = new InteractiveLabel("Speed interactiveLabel", labelText,
                                                    new Rectangle(midPanelFotText, diffY + 9 * textLabelHight,
                                                                  (int)diffBetweenLines.X,
                                                                  textLabelHight), GetPlayerCharacterTurnSpeed);
            interactiveLabel.SetOffset(new Vector2(Bounds.X, Bounds.Y));
            interactiveLabel.SetLabelTextColor(Color.Black);
            AddUIControl(interactiveLabel);

            //create "      Power :" interactiveLabel
            labelText = "Power: ";
            diffBetweenLines = Draws.CalculateWordSizeNormalFont(labelText);
            midPanelFotText = (int)(Bounds.Width - diffBetweenLines.X) / 2;


            interactiveLabel = new InteractiveLabel("Power interactiveLabel", labelText,
                                                    new Rectangle(midPanelFotText, diffY + 10 * textLabelHight,
                                                                  (int)diffBetweenLines.X,
                                                                  textLabelHight), GetPlayerCharacterTurnPower);
            interactiveLabel.SetOffset(new Vector2(Bounds.X, Bounds.Y));
            interactiveLabel.SetLabelTextColor(Color.Black);
            AddUIControl(interactiveLabel);
            return diffY;
        }

        private void CreateMouseOverLabels(int textLabelHight, Vector2 diffBetweenLines, int diffY) {
            string labelText;
            int midPanelFotText;
            Label label;

            diffY += (int)diffBetweenLines.Y;
            //            //create "under Mouse  Status :" label
            //            labelText = "Under Mouse \nResources";
            //            diffBetweenLines = Draws.CalculateWordSizeNormalFont(labelText);
            //            midPanelFotText = (int)(Bounds.Width - diffBetweenLines.X) / 2;
            //
            //            label = new Label("Resources left for Turn Status label", labelText,
            //                              new Rectangle(midPanelFotText, diffY + 11 * textLabelHight, (int)diffBetweenLines.X,
            //                                            textLabelHight));
            //            label.SetOffset(new Vector2(Bounds.X, Bounds.Y));
            //            AddUIControl(label);
            //
            //            diffY += (int)diffBetweenLines.Y;
            //
            //            //create "      Life :" label
            //            labelText = "Life : ";
            //            diffBetweenLines = Draws.CalculateWordSizeNormalFont(labelText);
            //            midPanelFotText = (int)(Bounds.Width - diffBetweenLines.X) / 2;
            //
            //            var interactiveLabel = new InteractiveLabel("Life label", labelText,
            //                                                        new Rectangle(midPanelFotText, diffY + 12 * textLabelHight,
            //                                                                      (int)diffBetweenLines.X,
            //                                                                      textLabelHight), new Func<string>(GetMouseOverLife));
            //            interactiveLabel.SetOffset(new Vector2(Bounds.X, Bounds.Y));
            //            interactiveLabel.SetLabelTextColor(Color.Red);
            //            AddUIControl(interactiveLabel);




            //create "      Character Type :" label
            labelText = "";
            diffBetweenLines = Draws.CalculateWordSizeNormalFont(labelText);
            midPanelFotText = (int)(Bounds.Width - diffBetweenLines.X) / 2;

            var interactiveLabel = new InteractiveLabel("Character Type label", labelText,
                                                        new Rectangle(10, Bounds.Height - 3 * textLabelHight,
                                                                      (int)diffBetweenLines.X,
                                                                      textLabelHight), new Func<string>(GetPlayerTypeUnderMouse));
            interactiveLabel.SetOffset(new Vector2(Bounds.X, Bounds.Y));
            interactiveLabel.SetLabelTextColor(Color.DarkRed);
            AddUIControl(interactiveLabel);


            //            //create "      Light :" label
            //            labelText = "Light : ";
            //            diffBetweenLines = Draws.CalculateWordSizeNormalFont(labelText);
            //            midPanelFotText = (int)(Bounds.Width - diffBetweenLines.X) / 2;
            //
            //            interactiveLabel = new InteractiveLabel("Light label", labelText,
            //                                                    new Rectangle(midPanelFotText, diffY + 13 * textLabelHight,
            //                                                                  (int)diffBetweenLines.X,
            //                                                                  textLabelHight), new Func<string>(GetMouseOverLight));
            //            interactiveLabel.SetOffset(new Vector2(Bounds.X, Bounds.Y));
            //            interactiveLabel.SetLabelTextColor(Color.Red);
            //            AddUIControl(interactiveLabel);
            //
            //
            //            //create "      Speed :" label
            //            labelText = "Speed : ";
            //            diffBetweenLines = Draws.CalculateWordSizeNormalFont(labelText);
            //            midPanelFotText = (int)(Bounds.Width - diffBetweenLines.X) / 2;
            //
            //            interactiveLabel = new InteractiveLabel("Speed label", labelText,
            //                                                    new Rectangle(midPanelFotText, diffY + 14 * textLabelHight,
            //                                                                  (int)diffBetweenLines.X,
            //                                                                  textLabelHight), new Func<string>(GetMouseOverSpeed));
            //            interactiveLabel.SetOffset(new Vector2(Bounds.X, Bounds.Y));
            //            interactiveLabel.SetLabelTextColor(Color.Red);
            //            AddUIControl(interactiveLabel);
            //
            //
            //            //create "      Power :" label
            //            labelText = "Power : ";
            //            diffBetweenLines = Draws.CalculateWordSizeNormalFont(labelText);
            //            midPanelFotText = (int)(Bounds.Width - diffBetweenLines.X) / 2;
            //
            //            interactiveLabel = new InteractiveLabel("Power label", labelText,
            //                                                    new Rectangle(midPanelFotText, diffY + 15 * textLabelHight,
            //                                                                  (int)diffBetweenLines.X,
            //                                                                  textLabelHight), new Func<string>(GetMouseOverPower));
            //            interactiveLabel.SetOffset(new Vector2(Bounds.X, Bounds.Y));
            //            interactiveLabel.SetLabelTextColor(Color.Red);
            //            AddUIControl(interactiveLabel);
        }

        #region DelgatedFunctions


        private static Texture2D GetCharacterImg() {
            //create Image for the character
            Texture2D img = null;
            string type = PlayerManager.Instance.CurrentPlayer.Property.PlayerType.ToString() + "s";

            switch (PlayerManager.Instance.CurrentPlayer.Property.CurrentCharacter.Type) {
                case CharacterType.Defender:
                    img = Character.VisualMap[new Key(type, "Defender", "walking", "sw")][0];
                    break;
                case CharacterType.Archer:
                    img = Character.VisualMap[new Key(type, "Archer", "walking", "sw")][0];
                    break;
                case CharacterType.Bomber:
                    img = Character.VisualMap[new Key(type, "Bomber", "walking", "sw")][0];
                    break;
                case CharacterType.None:
                    break;

            }

            return img;
        }

        private static Texture2D GetHumanCharacterImg() {
            throw new NotImplementedException();
        }


        private string GetPlayerTypeUnderMouse() {
            if (GamePanel.Current.CharactersInCellUnderMouse == null) return "";
            return GamePanel.Current.CharactersInCellUnderMouse.BelongsToPlayer.ToString() +
                " " +
                GamePanel.Current.CharactersInCellUnderMouse.Type.ToString() +
                "\n       Life : " +
                GamePanel.Current.CharactersInCellUnderMouse.InitialResources.Life
                ;
        }


        //        public string GetPlayerCharacterTurnLife() {
        //            return GamePanel.Current.Selected == null ? "" : GamePanel.Current.Selected.CurrentResources.Life.ToString();
        //        }



        public string GetPlayerCharacterTurnLight() {
            var ans = IsCharacterSelected() ? "" : PlayerManager.Instance.CurrentPlayer.Property.CurrentCharacter.CurrentResources.Light.ToString();
            return ans;
        }

        public string GetPlayerCharacterTurnSpeed() {
            return IsCharacterSelected() ? "" : PlayerManager.Instance.CurrentPlayer.Property.CurrentCharacter.CurrentResources.Speed.ToString();
        }

        public string GetPlayerCharacterTurnPower() {
            return IsCharacterSelected() ? "" : PlayerManager.Instance.CurrentPlayer.Property.CurrentCharacter.CurrentResources.Power.ToString();
        }

        public string GetPlayerCharacterLife() {
            return IsCharacterSelected() ? "" : PlayerManager.Instance.CurrentPlayer.Property.CurrentCharacter.InitialResources.Life.ToString();
        }

        public string GetPlayerCharacterLight() {
            return IsCharacterSelected() ? "" : PlayerManager.Instance.CurrentPlayer.Property.CurrentCharacter.InitialResources.Light.ToString()
                + "(" + GetPlayerCharacterTurnLight() + ")";
        }

        public string GetPlayerCharacterSpeed() {
            return IsCharacterSelected() ? "" : PlayerManager.Instance.CurrentPlayer.Property.CurrentCharacter.InitialResources.Speed.ToString()
                + "(" + GetPlayerCharacterTurnSpeed() + ")";
        }

        public string GetPlayerCharacterPower() {
            return IsCharacterSelected() ? "" : PlayerManager.Instance.CurrentPlayer.Property.CurrentCharacter.InitialResources.Power.ToString()
                + "(" + GetPlayerCharacterTurnPower() + ")";
        }

        private static bool IsCharacterSelected() {
            return PlayerManager.Instance.CurrentPlayer.Property.CurrentCharacter == NullCharacter.Instance || PlayerManager.Instance.CurrentPlayer.Property.CurrentCharacter == NullCharacter.Instance;
        }


        public string GetMouseOverLife() {
            if (GamePanel.Current.CharactersInCellUnderMouse == null) return "";
            return GamePanel.Current.CharactersInCellUnderMouse.InitialResources.Life.ToString();
        }
        //
        //        public string GetMouseOverLight() {
        //            return GamePanel.Current.CharactersInCellUnderMouse == null ? "" : GamePanel.Current.CharactersInCellUnderMouse.CurrentResources.Light.ToString();
        //        }
        //
        //        public string GetMouseOverPower() {
        //            return GamePanel.Current.CharactersInCellUnderMouse == null ? "" : GamePanel.Current.CharactersInCellUnderMouse.CurrentResources.Power.ToString();
        //        }
        //
        //        public string GetMouseOverSpeed() {
        //            return GamePanel.Current.CharactersInCellUnderMouse == null ? "" : GamePanel.Current.CharactersInCellUnderMouse.CurrentResources.Speed.ToString();
        //        }



        #endregion
    }
}